package com.tebiecloud.act.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.act.client.model.entity.ActGoodsRef;
import com.tebiecloud.act.server.service.ActGoodsRefService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 * 活动关联的商品表 前端控制器
 * @author tebie
 * @date 2019-09-24
 */
@RestController
@RequestMapping("/actgoodsref")
public class ActGoodsRefController {

    @Autowired
    private ActGoodsRefService actGoodsRefService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "list")
    public ResultBody<IPage<ActGoodsRef>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        ActGoodsRef query = pageParams.mapToObject(ActGoodsRef.class);
        QueryWrapper<ActGoodsRef> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(actGoodsRefService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("get")
    public ResultBody<ActGoodsRef> get(@RequestParam("id") Long id) {
        ActGoodsRef entity = actGoodsRefService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param actId 活动id,
     * @param goodsId 商品id,
     * @param specId 规格id,
     * @param price 售价（单位：分）,
     * @param retailPrice 门市价（单位：分）,
     * @param costPrice 成本价（单位：分）,
     * @param commissionRate 佣金比例（单位：/100=百分比）,
     * @param stockQty 库存数量：-99为不限库存,
     * @param deleted 是否删除：0否，1是
     * @return
     */
    @PostMapping("add")
    public ResultBody add(
        @RequestParam(value = "actId", required = false) Long actId,
        @RequestParam(value = "goodsId", required = false) Long goodsId,
        @RequestParam(value = "specId", required = false) Long specId,
        @RequestParam(value = "price", required = false) Integer price,
        @RequestParam(value = "retailPrice", required = false) Integer retailPrice,
        @RequestParam(value = "costPrice", required = false) Integer costPrice,
        @RequestParam(value = "commissionRate", required = false) Integer commissionRate,
        @RequestParam(value = "stockQty", required = false) Integer stockQty,
        @RequestParam(value = "deleted", required = false) Integer deleted
    ) {
        ActGoodsRef entity = new ActGoodsRef();
        entity.setActId(actId);
        entity.setGoodsId(goodsId);
        entity.setSpecId(specId);
        entity.setPrice(price);
        entity.setRetailPrice(retailPrice);
        entity.setCostPrice(costPrice);
        entity.setCommissionRate(commissionRate);
        entity.setStockQty(stockQty);
        entity.setDeleted(deleted);
        actGoodsRefService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param actId 活动id
     * @param goodsId 商品id
     * @param specId 规格id
     * @param price 售价（单位：分）
     * @param retailPrice 门市价（单位：分）
     * @param costPrice 成本价（单位：分）
     * @param commissionRate 佣金比例（单位：/100=百分比）
     * @param stockQty 库存数量：-99为不限库存
     * @param deleted 是否删除：0否，1是
     * @return
     */
    @PostMapping("update")
    public ResultBody update(
            @RequestParam(value = "actGoodsId", required = false) Long actGoodsId,
            @RequestParam(value = "actId", required = false) Long actId,
            @RequestParam(value = "goodsId", required = false) Long goodsId,
            @RequestParam(value = "specId", required = false) Long specId,
            @RequestParam(value = "price", required = false) Integer price,
            @RequestParam(value = "retailPrice", required = false) Integer retailPrice,
            @RequestParam(value = "costPrice", required = false) Integer costPrice,
            @RequestParam(value = "commissionRate", required = false) Integer commissionRate,
            @RequestParam(value = "stockQty", required = false) Integer stockQty,
            @RequestParam(value = "deleted", required = false) Integer deleted
    ) {
        ActGoodsRef entity = new ActGoodsRef();
        entity.setActGoodsId(actGoodsId);
        entity.setActId(actId);
        entity.setGoodsId(goodsId);
        entity.setSpecId(specId);
        entity.setPrice(price);
        entity.setRetailPrice(retailPrice);
        entity.setCostPrice(costPrice);
        entity.setCommissionRate(commissionRate);
        entity.setStockQty(stockQty);
        entity.setDeleted(deleted);
        actGoodsRefService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            actGoodsRefService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            actGoodsRefService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
